<template>
	<form class='loginView' @submit="login">
		<view class="input-view">
			<view class="label-view">
				<text class="label">手机号 </text>
			</view>
			<input class="input" v-model="user.phone" type="text" placeholder="请输入用户名" />
		</view>
		<view class="button-view">
			<button type="default" class="login" hover-class="hover" formType="submit">登录</button>
		</view>
	</form>
</template>

<script>
	import { loginApi, wxLogin } from "../../http/login.js"
	import {setUserInfo} from "@/utils/selfLocalStorage.js"
	// import { wxLogin } from "@/utils/requests.js"
	
	export default {
		data() {
			return {
				user: {
					phone:""
				}
			};
		},
		methods: {
			checkForm(){
				// 校验手机号
				let phoneTest = /^1[3-9]\d{9}$/
				if(!phoneTest.test(this.user.phone)){
					uni.showToast({
						title:'手机号格式错误',
						icon:'none',
						duration:2000
					})
					return false;
				}
				return true;
			},
			
			async login(e) {
				if(!this.checkForm()){
					return;
				}
				const res = await wxLogin(this.user.phone);
				if(res){
					setUserInfo(res.data);
					uni.navigateBack();
				}
				// const data = await loginApi(this.user);	
				// if(data){
				// 	// 保存数据数据
				// 	setUserInfo(data)
				// 	// 重定向
				// 	uni.navigateBack();
				// }
			},
			register() {
				// 清空表单
				this.user.username = '';
				this.user.password = '';
				uni.navigateTo({
					url: "/pages/login/register"
				})
			}
		}
	}
</script>

<style>

</style>